<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="理财名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入理财名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="理财状态" prop="status">
        <dict-select :options="dict.type.my_financial_status" v-model="queryParams.status"
                     placeholder="请选择打款方式"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="detailList">
      <el-table-column label="用户名" align="center" prop="userName" min-width="180"/>
      <el-table-column label="理财" align="center" prop="name" min-width="180" />
      <el-table-column label="本金" align="center" prop="amount" min-width="150px" :show-overflow-tooltip="true"/>
      <el-table-column label="周期(天)" align="center" prop="period" min-width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="投资时间" align="center" prop="createTime" min-width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{m}/{d}/{y}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="settlementTime" min-width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settlementTime, '{m}/{d}/{y}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总收益" align="center" prop="totalAmount" min-width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ $yun.priceFormatter(scope.row.totalAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投资合同" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder"
            @click="handleShow(scope.row)"
            v-hasPermi="['car:detail:edit']"
          >查看合同
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="合同" :visible.sync="open" width="800px" append-to-body>
      <div v-html="contract" style="max-height: 700px;overflow-y: scroll"></div>
    </el-dialog>
  </div>
</template>
<script>
import { listDetail, contract } from '@/api/financial/onlineHistory'

export default {
  name: 'Detail',
  dicts: ['my_financial_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      detailList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //合同
      contract:null,
      // 查询参数
      queryParams: {
        userName:null,
        name:null,
        status:null,
        pageNum: 1,
        pageSize: 10
      },
      indexArr:[],
      indexPos:0,
    }
  },
  created() {
    this.getList()
    // this.objectSpanMethod();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      console.log(this.queryParams)
      listDetail(this.queryParams).then(response => {
        this.detailList = response.data.records
        this.total = response.data.total
        this.loading = false
        for (var i = 0; i < this.detailList.length; i++) {
          if (i === 0) {
            //给当前数组添加一个1现在数组为[1]代表第一个各自占一行
            this.indexArr.push(1)
            //当前索引为0
            this.indexPos = 0
          } else {
            //判断当前元素与上一个元素是否相同
            if (this.detailList[i].userName === this.detailList[i - 1].userName) {
              //给当前素引下合并的格子数量 自增1 例如 现在是[1]增加用后变为[2]代表现在合并的格子数量为2
              this.indexArr[this.indexPos] += 1
              //给数组添加一个0因为有格子合并了他的位置当前被合并的格子需要隐藏, 所以当前被合并的格子占0行是隐藏状态,数组为[2,0]
              this.indexArr.push(0)
            }else
              {
                //当两行的数据不相同时 则给新的数据 合并的格子数量为1
                this.indexArr.push(1)
                //将当前索引 赋值为 便于this.indexArr[this.indexPos]修改相对应索引的合并的格子数量
                this.indexPos = i
              }
            }
          }
      })
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.indexArr[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col,
        }
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 查看看合同 */
    handleShow(row) {
      contract(row.id).then(res=>{
        this.contract = res.data;
        this.open = true;
      })
    }
  }
}
</script>
